package cn.thoughtworks.school.controllers;

import cn.thoughtworks.school.commands.CreateUserCommand;
import cn.thoughtworks.school.exceptions.BusinessException;
import cn.thoughtworks.school.services.UserCenterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping(value = "/api/users")
public class UserController {

  @Autowired
  private UserCenterService userCenterService;

  @PostMapping("/register")
  public ResponseEntity getUserById(@RequestBody CreateUserCommand createUserCommand) throws BusinessException {
    Map user = userCenterService.register(createUserCommand);
    return new ResponseEntity(user, HttpStatus.CREATED);
  }

  @PutMapping("")
  public ResponseEntity joinActivity(@RequestBody CreateUserCommand createUserCommand) throws BusinessException {
    userCenterService.joinActivity(createUserCommand);
    return new ResponseEntity(HttpStatus.NO_CONTENT);
  }

  @PostMapping("/login")
  public ResponseEntity usernameLogin(@RequestBody CreateUserCommand user) throws BusinessException {
    Map loginUser = userCenterService.usernameLogin(user);
    return ResponseEntity.ok(loginUser);
  }

}
